package com.ctech.wsgen.writer;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Element;

import com.ctech.wsgen.model.Item;
import com.ctech.wsgen.model.Manufacture;
import com.ctech.wsgen.model.Site;

public class ManufactureListingDom4JHtmlWriter extends AbstractItemListingDom4JHtmlWriter {
	
	private Logger log = Logger.getLogger(ManufactureListingDom4JHtmlWriter.class.getName());
	
	public static final String pageName = "Manufacture List"; 
	public static final String filename = "Manufacture_Price_List_Alphabetical.htm"; 
	public static final String metaContentName = "Manufacture List";
	
	List<Manufacture> manufactures;
	Site site;
	
	public ManufactureListingDom4JHtmlWriter(List<Manufacture> manufactures, Site site) throws Exception {
		
		if(manufactures != null && manufactures.size() != 0) {
			this.manufactures = manufactures;
			this.site = site;
			File file = new File(site.getSiteDir().getAbsolutePath().concat(File.separator + filename));
			if(!file.exists()) file.createNewFile();
			createHtml(file, this.site.getJavaScriptBlock());		
			if(log.isInfoEnabled()) log.info("Created " + filename + " with " + manufactures.size() + " Manufacture Listings");
		} else {
			log.error("No Manufactures to make " + filename);
		}
		
	}

	@Override
	public void createContentFooter(Element divContent) {
	}

	@Override
	public String getContentHeaderTitle() {
		return pageName;
	}

	@Override
	public List<Item> getItems() {
		return null;
	}

	@Override
	public String getMetaContentName() {
		return metaContentName;
	}

	@Override
	public String getTitleName() {
		return pageName;
	}

	@Override
	public void createDocContainer(Element divContainer) {
		/*
		<div id="Container">
			<div id="alphaLinks">
				<a href="Manufacture_Price_List_Alphabetical.htm#3">3</a>
				<a href="Manufacture_Price_List_Alphabetical.htm#A">A</a>
				<a href="Manufacture_Price_List_Alphabetical.htm#B">B</a>
				...
				<a href="Manufacture_Price_List_Alphabetical.htm#Z">Z</a>
			</div>
		*/
		Element alphaLinksDiv = divContainer.addElement("div").addAttribute("id", "alphaLinks");
		
		/*
			<div id="SideBar">		
				<table cellspacing="4">
				  <tr>
				    <td class=GroupName nowrap><a name="3" />3</td>
			      </tr>
				  <tr>
		
				    <td class=Navigation nowrap><a href=./3M_Electrical_Products_Pricelist.htm>3M Electrical Products Division</a></td>
			      </tr>
				  <tr>
				    <td class=GroupName nowrap><a name="A" />A</td>
			      </tr>
				  <tr>
				    <td class=Navigation nowrap><a href="ABC_Cable_Pricelist.htm">ABC</a></td>
		
			      </tr>
				  <tr>
				    <td class=Navigation nowrap><a href=./Accutech_Innovative_Control_Systems_Pricelist.htm>Accutech / Innovative Control Systems</a></td>
			      </tr>
		 */
		// create the SideBar div and table 
		Element sidebarDivTable = divContainer
			.addElement("div").addAttribute("id", "SideBar")
				.addElement("table").addAttribute("cellspacing", "4");
		
		String alphaChar = "";
		for (Manufacture manufacture : manufactures) {
			if(!alphaChar.equalsIgnoreCase(manufacture.getVendorNameFirstChar())) {
				alphaChar = manufacture.getVendorNameFirstChar();
				// create the alphaLink row
				alphaLinksDiv.addElement("a").addAttribute("href", filename + "#" + alphaChar).addText(alphaChar);
				
				// create the row for GroupName
				sidebarDivTable
					.addElement("tr")
						.addElement("td").addAttribute("class", "GroupName").addAttribute("nowrap", "nowrap")
							.addElement("a").addAttribute("name", alphaChar).addText(alphaChar);
			}
			// crete the row for Manufacture Link
			sidebarDivTable
				.addElement("tr")
					.addElement("td").addAttribute("class", "Navigation").addAttribute("nowrap", "nowrap")
						.addElement("a").addAttribute("href", "./" + manufacture.getFileName())
							.addText(manufacture.getVendorName());
							//+ " - (" + manufacture.getItems().size() + ") " + (manufacture.getWebsiteURL() == null ? "No Website URL" : ""));
		}
		
		
		// TESTING 
		// A
		/*
		alphaLinksDiv.addElement("a").addAttribute("href", filename + "#A").addText("A");
		sidebarDivTable
		.addElement("tr")
			.addElement("td").addAttribute("class", "GroupName").addAttribute("nowrap", "nowrap")
				.addElement("a").addAttribute("name", "A").addText("A");
		sidebarDivTable
		.addElement("tr")
			.addElement("td").addAttribute("class", "Navigation").addAttribute("nowrap", "nowrap")
				.addElement("a").addAttribute("href", "ABC_Cable_Pricelist.htm").addText("ABC");
		sidebarDivTable
		.addElement("tr")
			.addElement("td").addAttribute("class", "Navigation").addAttribute("nowrap", "nowrap")
				.addElement("a").addAttribute("href", "./Accutech_Innovative_Control_Systems_Pricelist.htm").addText("Accutech / Innovative Control Systems");
		
		alphaLinksDiv.addElement("a").addAttribute("href", filename + "#B").addText("B");
		alphaLinksDiv.addElement("a").addAttribute("href", filename + "#C").addText("C");
		*/
	}

	@Override
	public String createMetaTagItemContent() {
		StringBuffer content = new StringBuffer();
		if(manufactures != null) {
			for (Manufacture manufacture : manufactures) {
				content.append(manufacture.getVendorName() + " , ");
			}
		}
		return content.toString();
	}

	@Override
	public String getEmailAddress() {
		return this.site.getStr_email();
	}

	@Override
	public String getWebsiteURL() {
		return this.site.getStr_websiteUrl();
	}

}
